home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / more13.zip / STRINGS.C < prev    next >
Text File  |  1990-06-24  |  2KB  |  137 lines

  1. /* Strings.C
  2.  *
  3.  * Displays a list of all strings in a file read from a file that
  4.  * are entirely ASCII -- that is, between 32 and 126. Must be at least four
  5.  * characters long, or however many as specified by an argument to the
  6.  * program.
  7.  *
  8.  * Syntax: strings <filename> [<min_chars>] [/X]
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <io.h>
  14. #include <string.h>
  15.  
  16. #define SYNMSG        "strings: syntax <filename> [<min_chars>] [/X]"
  17. #define NOPMSG        "strings: unable to open file"
  18. #define NOSTOR        "strings: unable to allocate storage"
  19.  
  20. char copyright[] = "STRINGS v1.1 dated " __DATE__ ", Copyright 1990 by "
  21.                 "Daniel Sachs.";
  22.  
  23. void main(int argc, char *argv[])
  24. {
  25.     int min_chars;
  26.     int cur_char;
  27.     unsigned long pos;
  28.     unsigned long beg;
  29.     FILE *fp;
  30.     char *tmp;
  31.     char c;
  32.     int printed;
  33.     int hexflag;
  34.  
  35.     if( (argc < 2) || (argc > 4) )
  36.     {
  37.         printf(SYNMSG);
  38.         exit(2);
  39.     }
  40.  
  41.     if( argc == 2 && !stricmp(argv[1],"/C") )
  42.     {
  43.         printf(copyright);
  44.         exit(0);
  45.     }
  46.  
  47.     if( argc == 3 && stricmp(argv[2],"/X") )
  48.         min_chars = atoi(argv[2]);
  49.     else
  50.         min_chars = 4;
  51.  
  52.     if( argc == 4 )
  53.         min_chars = atoi(argv[2]);
  54.  
  55.     if( min_chars == 0 )
  56.     {
  57.         printf(SYNMSG);
  58.         exit(2);
  59.     }
  60.  
  61.     if( !stricmp(argv[argc-1],"/X") )
  62.         hexflag = 1;
  63.     else
  64.         hexflag = 0;
  65.  
  66.     if( argc == 4 && !hexflag)
  67.     {
  68.         printf(SYNMSG);
  69.         exit(2);
  70.     }
  71.  
  72.     fp = fopen(argv[1],"rb");
  73.  
  74.     if( fp == NULL )
  75.     {
  76.         printf(NOPMSG);
  77.         exit(2);
  78.     }
  79.  
  80.     tmp = malloc(min_chars+1);
  81.  
  82.     if( tmp == NULL )
  83.     {
  84.         printf(NOSTOR);
  85.         exit(2);
  86.     }
  87.  
  88.     printf("Offset\tString\n");
  89.  
  90.     cur_char = 0;
  91.     beg = 0;
  92.     printed = 0;
  93.  
  94.     for(pos = 0;;pos++)
  95.     {
  96.         fread(&c,1,1,fp);
  97.  
  98.         if( feof(fp) )
  99.             break;
  100.  
  101.         else
  102.         {
  103.             if( (c > 31) && (c < 127) )
  104.             {
  105.                 cur_char++;
  106.                 if (cur_char > min_chars)
  107.                     putchar(c);
  108.                 if (cur_char == min_chars)
  109.                 {
  110.                     printf(hexflag ? "%lX\t%s%c" :
  111.                         "%li\t%s%c", beg, tmp, c);
  112.                     printed = 1;
  113.                 }
  114.                 if (cur_char < min_chars)
  115.                 {
  116.                     *(tmp + (cur_char-1)) = c;
  117.                     *(tmp + cur_char    ) = 0;
  118.                 }
  119.                 if (cur_char == 1)
  120.                     beg = pos;
  121.             }
  122.             else
  123.             {
  124.                 if( cur_char != 0 )
  125.                 {
  126.                     cur_char = 0;
  127.                     if (printed)
  128.                     {
  129.                         printf("\n");
  130.                         printed = 0;
  131.                     }
  132.                 }
  133.             }
  134.         }
  135.     }
  136. }
  137.